package cn.com.scitc.licenseplateback.controller;

import cn.com.scitc.licenseplateback.dao.MembershipInformationDao;
import cn.com.scitc.licenseplateback.dateFormat.DateFormat;
import cn.com.scitc.licenseplateback.identityVerification.IdentityVerification;
import cn.com.scitc.licenseplateback.md5.MD5Util;
import cn.com.scitc.licenseplateback.model.RegisterEntity;
import cn.com.scitc.licenseplateback.redis.RedisUtil;
import cn.com.scitc.licenseplateback.verificationCode.RequestData;
import cn.com.scitc.licenseplateback.verificationCode.SendSms;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
public class Register {

    /**
     * 存储手机号、产生的验证码
     */
    @Autowired
    private RequestData requestData;

    /**
     * 发送验证码到手机、并将验证码存储在redis
     */
    @Autowired
    private SendSms sendSms;

    /**
     * 用户身份证号和姓名有效性验证
     */
    @Autowired
    private IdentityVerification identityVerification;

    /**
     * 信息双重加密库
     */
    @Autowired
    private MD5Util md5Util;

    /**
     * 日期时间库
     */
    @Autowired
    private DateFormat dateFormat;

    /**
     * 会员信息对象
     */
    @Autowired
    private MembershipInformationDao membershipInformationDao;


    /**
     * 缓存库
     */
    @Autowired
    private RedisUtil redisUtil;


    /**
     * 获取短信验证码
     *
     * @param phoneNumber
     */
    @PostMapping("/getSMS")
    private String getSMS(String phoneNumber) {
        requestData.setPhoneNumbers(phoneNumber);

        String code = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            code += random.nextInt(10);
        }

        requestData.setTemplateParam(code);
        String statusText = sendSms.send(requestData);
        if (statusText != "") return statusText;//短信发送失败
        return "短信发送成功.";
    }

    /**
     * 用户注册
     *
     * @param registerInfo
     * @return
     */
    @PostMapping("/register")
    private String register(String registerInfo) {
        //将页面提交到后端JSON字符串数据转换为 JSON 对象
        JSONObject jsonObject = JSONObject.parseObject(registerInfo);
        //将 JSON 对象转换为 JAVA 对象
        RegisterEntity registerEntity = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<RegisterEntity>() {
        });

        // 手机验证码验证
        String phoneVer = redisUtil.get(registerEntity.getCellPhoneNumber()).toString();

        if (StringUtils.isBlank(phoneVer)){
            return "请获取短信验证码";
        }

        if (!phoneVer.equals(registerEntity.getCaptcha())){
            return "验证码有误";
        }

        // 简单密码验证是否一致
        if (!registerEntity.getPassword().equals(registerEntity.getConfirmPassword())){
            return "密码格式错误";
        }

        // 身份证有效性验证
        boolean b = checkIdName(registerEntity);
        // 若身份验证失败
        if (!b){
            return "身份信息错误";
        }

        // 会员用户密码加密
        String[] encryption = encryption(registerEntity);

        // 将加密后的密码重新存入对象中
        registerEntity.setPassword(encryption[0]);
        registerEntity.setConfirmPassword(encryption[0]);

        // 将加密盐放入对象中
        registerEntity.setStal(encryption[1]);

        // 获取当前时间
        String currentDateTime = dateFormat.getCurrentDateTime();
        registerEntity.setRegisterTime(currentDateTime);

        // 完成用户注册
        int i = membershipInformationDao.addMember(registerEntity);

        return String.valueOf(i);
    }

    /**
     * 用户注册时，对姓名和身份证号进行验证
     * @param registerEntity
     * @return
     */
    private boolean checkIdName(RegisterEntity registerEntity){
        return identityVerification.identityVerification(registerEntity.getId().toString(), registerEntity.getName().toString());
    }

    /**
     * 会员用户密码进行二次双重加密 - 一次加密经客户端加密
     * @param registerEntity
     */
    private String [] encryption(RegisterEntity registerEntity){
        String stal = md5Util.randomSalt();
        return new String[]{md5Util.FINALPASS(registerEntity.getPassword().toString(), stal), stal};
    }

}
